#include "our_header.h"
#include <stdio.h>

// 切割字符串
void parseDir(char *dir_path, LStack_7th *stack);

// 拼接字符串
int strcatDir(LStack_7th *stack, char *dest_dir);

int calDestPath(char curpth[], char destpath[], char resltpath[]) {
	LStack_7th stack = {0};
	parseDir(curpth, &stack);
	// 处理dest_dir;
	char *token, *saveptr;
	token = strtok_r(destpath, "/", &saveptr);
	while(token) {
		DT_7th it = {0};
		if(strcmp(token, "..") == 0) {
			pullLStack_7th(&stack, &it);
		} else if(strcmp(token, ".")) {
			it.dir_item = token;
			pushLStack_7th(&stack, &it);
		}
		token = strtok_r(NULL, "/", &saveptr);
		if(stack.size <= 0) {
			freeLSTack_7th(&stack);
			return -1;
		}
	}
	strcatDir(&stack, resltpath);
	freeLSTack_7th(&stack);
	return 0;
};

void parseDir(char *dir_path, LStack_7th *stack) {
	initLStack_7th(stack);
	char *token, *saveptr;
	token = strtok_r(dir_path, "/", &saveptr);
	while(token != NULL) {

		DT_7th item = {0};
		item.dir_item = token;
		pushLStack_7th(stack, &item);
		token = strtok_r(NULL, "/", &saveptr);
	}
}

int strcatDir(LStack_7th *stack, char *dest_dir) {
	if(stack == NULL || stack->size <= 0 || dest_dir == NULL)
		return -1;

	DT_7th it = {0};
	LStack_7th stack1;
	initLStack_7th(&stack1);
	while(pullLStack_7th(stack, &it) == 0) {
		pushLStack_7th(&stack1, &it);
	}

	while(pullLStack_7th(&stack1, &it) == 0) {
		strcat(dest_dir, it.dir_item);
		if(stack1.size)
			strcat(dest_dir, "/");
	}
	freeDTitem_7th(&it);
	return 0;
}